package com.sport.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.sport.bean.Admin;
import com.sport.bean.PageParam;
import com.sport.bean.PageResult;
import com.sport.bean.Student;
import com.sport.service.IStudentService;
import com.sport.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class StudentController {
    @Autowired
    private IStudentService studentService;


    /*
    分页查询管理员
     */
    @GetMapping(value = "/student/list")
    @ResponseBody
    public PageResult<Student> findAll1(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                        @RequestParam(value = "limit", defaultValue = "10") Integer limit,
                                        HttpServletRequest request) {
        PageHelper.startPage(page, limit);
        PageParam<Student> pageParam = new PageParam<>(request);
        List<Student> students = studentService.listPage(pageParam);
        PageInfo info = new PageInfo(students);
        return new PageResult<Student>(students, info.getTotal());
    }

    @GetMapping(value="/student/lists", produces="text/html;charset=UTF-8")
    @ResponseBody
    public String lists() {
        Map<String,Object> map =new HashMap<>();
        map.put("code",0);
        map.put("data",studentService.listStudent());
        Gson gson= new Gson();
        String json=gson.toJson(map);

        return json;
    }

    /*
    删除
     */
    @GetMapping("/student/del")
    @ResponseBody
    public JsonResult delAdmin(Integer id) {
        if (studentService.deletStudent(id) > 0) {
            return JsonResult.ok("删除成功");
        } else {
            return JsonResult.error("删除失败");
        }
    }


    /**
     id查找
     */
    @GetMapping("/student/findId")
    @ResponseBody
    public Student findId(Integer id) {
        int i=id;
        System.out.println(studentService.findById(id));
        return studentService.findById(id);
    }
    /**
     修改学生
     */
    @GetMapping("/stu/update")
    @ResponseBody
    public JsonResult updatStu(MultipartFile file, Student student) {
        if ( studentService.updateInfo(student) > 0) {
            return JsonResult.ok("修改成功");
        } else {
            return JsonResult.error("修改失败");
        }

    }
    /**
     添加学生
     */
    @GetMapping("/stu/add")
    @ResponseBody
    public JsonResult addStu(MultipartFile file, Student student) {

        if ( studentService.addStudent(student) > 0) {
            return JsonResult.ok("添加成功");
        } else {
            return JsonResult.error("添加失败");
        }

    }

    /**
     判断学号是否重复
     */
    @GetMapping("/stu/findByStuno")
    @ResponseBody
    public JsonResult findByStuno(String stuno) {
        if (studentService.findByStuno(stuno)!=null){
            return JsonResult.error("学号已存在");
        }else {
            return null;
        }
    }
}